
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
	<meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Test XSeq</title>
    <script src="xlib.js"></script>
	<style>
	</style>
  </head>  
  <body>
    <div id="result"></div>
    <script>
const match = XLIB.match
let error_count = 0, tc_count = 0, log = ""

function assert(criteria, fail_msg, succ_msg=""){
  tc_count++
  
  if( criteria ){
    log += `<p>${succ_msg}</p>`
  }else{
    error_count++
    log += `<p>${fail_msg}</p>`
    console.error( fail_msg )
  }
}

// ======== 简单类型匹配
{
  const m = new Map()   // 不匹配透传原始数据
  for( let [i,o] of [[true, "true"], [123, "123"], [3.1415, "3.1415"], ["Oscar", "Hello,world!"], [m,m]] ){
    let res = match(i)(
      [XLIB.bool, "true"],
      [XLIB.int, "123"],
      [XLIB.num, "3.1415"],
      [XLIB.str, "Hello,world!"],
    )  
    assert(res===o, `简单类型匹配测试失败: ${res}!=${o}`)
  }
}

// ========  Default测试
{
  let res = match(123)([XLIB.str, "string"], 54321)
  assert(res===54321, `Default测试失败：${res}!=54321`)
}

// ========  Corner处理
{
  let res = match(123)()
  assert(res===123, `无条件Corner测试失败: ${res}!=123`)
}


//========================================================================================================================
if( error_count==0 ) console.log(`%cSuccess: All Test Case Pass!`, 'color:green')

const rst = document.getElementById("result")
rst.appendChild(document.createTextNode(`成功率: ${tc_count-error_count}/${tc_count}`))
const logE = document.createElement("div")
logE.innerHTML = log
rst.appendChild(logE)

    </script>
  </body>
</html>
    
